package com.myutils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

/**
 * @author Lan
 * @Company 上海全景数字技术有限公司
 */
public class TarUtil {

	protected static Logger logger = Logger.getLogger(TarUtil.class);

	/**
	 * 将一组文件打成tar包
	 * 
	 * @author Lan
	 * @param baseDir
	 *            基础目录
	 * @param sources
	 *            要打包的原文件集合（相对于baseDir的路径：hdewb_qj/xxx.html）
	 * @param target
	 *            打包后的文件
	 * @return
	 */
	public static boolean archiveTar(String baseDir,
			Collection<String> sources, File target) {
		boolean success = true;
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(target);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error("目标Tar文件不存在");
			return false;
		}

		TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(out);

		for (String filePath : sources) {
			TarArchiveEntry entry = new TarArchiveEntry(filePath);
			String fileAbsolutePath = baseDir + File.separator + filePath;
			File file = new File(fileAbsolutePath);
			entry.setSize(file.length());
			try {
				tarOutput.putArchiveEntry(entry);
				IOUtils.copy(new FileInputStream(file), tarOutput);
				tarOutput.closeArchiveEntry();
			} catch (IOException e) {
				e.printStackTrace();
				logger.error("文件归档失败[" + filePath + "]");
				success = false;
			}
		}

		if (tarOutput != null) {
			try {
				tarOutput.flush();
				tarOutput.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		logger.debug("归档文件生成路径：" + target.getAbsolutePath());
		return success;
	}

	/**
	 * 
	 * @Title: compress
	 * @Description: 将文件用gzip压缩
	 * @param source
	 *            需要压缩的文件
	 * @return File 返回压缩后的文件
	 * @throws
	 */
	public static File compress(File source) {
		File target = new File(source.getName() + ".gz");
		FileInputStream in = null;
		GZIPOutputStream out = null;
		try {
			in = new FileInputStream(source);
			out = new GZIPOutputStream(new FileOutputStream(target));
			byte[] array = new byte[1024];
			int number = -1;
			while ((number = in.read(array, 0, array.length)) != -1) {
				out.write(array, 0, number);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}

			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
		}
		return target;
	}
}
